#!/usr/bin/perl

# Here we have five conditions
# reach maxconn | http react | http fail | vip in | vip out

use strict ;

my $file = "slbalarm.cache";
my $logger="/bin/logger";

my $time_limit = 300;
my $alarm_limit = "3";
my @exceptions = ("SEDNA", "SENSEI");
#my @exceptions = "";

my @out = "";
my @out_unorder = "";
my @out_order = "";
my $this_time = ();
my $timestamp = time();
my $diff_time = ();
my $counter = ();
my $saida = ();
my %hash = ();
my $bypass;

my $logfile = "/export/logs/slb_alarms.log" ;

sub runLogger{
	my $line = shift ();
	`$logger -p local5.info "$line"`;

	open ( LOG, ">>$logfile" ) or die "Can't open $logfile: $!\n" ;
		print LOG "$line";
	close (LOG);
	print "$line";
}

while ( <STDIN> ) {
	chomp;
	my $check_line = $_ ;

	my @line = split (/\s|\t/,"$_");
	@line = @line[15,16,17,18,19,20,21,22,23,24,25,26];
	my $firstfield = shift(@line);

	#if ( $check_line =~ /.*server\s([\d|\.]+)\:.*serverfarm\'(.*)\'.*has\sreached\s\sconfigured\smax-conns/ ) 
	if ( $check_line =~ /.*server\s([\d|\.]+)\:.*serverfarm\s\'(.*)\'.*max-conns/ ) {
   		# reach maxconn 
		#print "$check_line\n";
		my $error = "$1 $2 has reached configured max-conss";

		#print "$error\n" ;
		push (@out, "$error");
	}   
  	elsif ($firstfield =~ /^SLB/) {
		if ($line[3] =~ /activated/ ) {
		# http reactivated
		#print "@line\n";
		}

		if ($line[3] =~ /failed/ ) {
		# http failed
		#print "@line\n";
		my $error = "$line[9] $line[2] $line[3] to $line[6]";
		push (@out, "$error");
		}
	}
	else {
		if ($line[0] eq "inservice" ) {
		# vip in service
		#print "$firstfield @line\n";
		}
		else {
		# vip out of service
		#print "$firstfield @line\n";
		}
	}

	foreach (@out) {
	        $this_time = time();
		$diff_time = $this_time - $timestamp;
		chomp;
	        my ( $value, $farm, $status, $nop2, $ipaddr) = split(/\s/,$_);
	        $counter->{$farm}->{$ipaddr}++;   
	        if ($counter->{$farm}->{$ipaddr} > $alarm_limit){
			$counter->{$farm}->{$ipaddr} = "";

			foreach (@out_order) {
				if ($saida->{$farm}->{$ipaddr} = $_) {
					$bypass = 1;
				}
			}
	
			foreach my $exp(@exceptions) {
				if ($farm =~ m/$exp/) {
					$bypass = 1;
				}
			}
	
			if ($bypass eq "1") {
				next;
			}
			else {
				if ($_ ne "") {
		                	push (@out_unorder, "$_\<br\>\| ");
				}
				#print qq^push $farm $ipaddr\n^;
			}
		}

        	if ($diff_time > $time_limit) {
			$counter = ();
			$timestamp = time();
		        @hash{@out_unorder} = @out_unorder;
			for (keys %hash) { push (@out_order, "$_"); }
			@out_order = sort(@out_order);
			#print "$#out_order\n";
		
	        	if ($#out_order <= "1"){
				#print "ciclo -> OK\n";
				my $now = time ;
				runLogger("[$now] PROCESS_SERVICE_CHECK_RESULT;zula1;SLB alarm;0;Servidor OK\n");
			}
			else {
				#print "ciclo -> FAIL -> @out_order\n";
				my $now = time ;
				runLogger("[$now] PROCESS_SERVICE_CHECK_RESULT;zula1;SLB alarm;1;\<b\>@out_order\<\/b\>\n");
			}
	
			$saida = ();
			$counter = ();
			@out_unorder = "";
			@out_order = "";
		}
	}     
}
